<?php
/**
 * Created by PhpStorm.
 * Author: i@fedax.cn
 * Date: 2018/1/18
 * Time: 17:39
 */

namespace app\admin\controller;

use app\admin\model\User;
use app\admin\model\HotDoor as HotDoorModel;

/**
 * 热门管理
 * Class AppManage
 * @package app\admin\controller
 */
class HotDoor extends Index
{
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData()
    {
        $hotDoorModel = new HotDoorModel();
        $page_config = array(
            'page' => input('post.offset', 1),
            'list_rows' => input('post.limit', 10),
        );
        $order = input('post.sort', 'id') . " " . input('post.order', 'desc');
        $where = array();//组装搜索语句
        if (input('value')) {
            $where[] = [input('key'), '=', input('value')];
        }
        $data = $hotDoorModel->getDataList($page_config, $where, $order);
        return json($data);
    }


    /**
     * 添加
     * @return \think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (request()->isPost()) {
            $userModel = new User();
            $userDetail = $userModel->where('u_id', input('u_id'))->find();
            if (!in_array(input('hot_id'), array(1, 2, 3, 4))) {
                $this->error(lang('add') . lang('false') . '-' . '只能在1234中选择');
            }
            if (!$userDetail) {
                $this->error(lang('add') . lang('false') . '-' . '用户不存在');
            }
            $hotDoorModel = new HotDoorModel();
            if ($hotDoorModel
                ->where('user_id', $userDetail['id'])
                ->where('hot_id', input('hot_id'))
                ->where('start_time', input('start_time'))
                ->where('end_time', input('end_time'))
                ->find()) {
                $this->error(lang('add') . lang('false') . '-' . '数据重复');
            }
            $newData = array();
            $newData['user_id'] = $userDetail['id'];
            $newData['start_time'] = input('start_time');
            $newData['end_time'] = input('end_time');
            $newData['hot_id'] = input('hot_id');

            try {
                $hotDoorModel->save($newData);
            } catch (\Exception $e) {
                $this->error(lang('add') . lang('false') . $e->getMessage());
            }
            $this->success(lang('add') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * 删除
     * @return \think\response\Redirect
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (request()->isPost()) {
            $hotDoorModel = new HotDoorModel();
            if ($hotDoorModel->where('id', input('id'))->delete()) {
                $this->success(lang('delete') . lang('success'));
            }
            $this->error(lang('delete') . lang('false'));
        }
        return redirect('/Miss');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isPost()) {
            if (1 == 200) {
                $this->success(lang('edit') . lang('success'));
            }
            $this->error(lang('edit') . lang('false'));
        }
        return redirect('/Miss');
    }
}